/** File: gen_neg.cpp
 ** Author: Ryan Lei
 ** Creation: 2009/12/26
 ** Modification: 2010/04/03
 ** Description: Generate multiple negative images chopped
 **              from large ones.
 **/

/* Usage: gen_neg [INPUT_DIR] [OUTPUT_DIR] */

#include "util.h"

int main(int argc, char *argv[]) {
	DIR *dirin, *dirout;
	struct dirent *dp;
	char INPUT_PATH_BASE[ MAX_PATH_LENGTH ];
	char INPUT_PATH[ MAX_PATH_LENGTH ];
	char OUTPUT_PATH_BASE[ MAX_PATH_LENGTH ];
	char OUTPUT_PATH[ MAX_PATH_LENGTH ];
	char slash = Unix ? '/' : '\\'; // It is '/' or '\' depending on OS

	if (argc != 3) {
		error("Usage: gen_neg [INPUT_DIR] [OUTPUT_DIR].");
	}

	if (!(dirin = opendir(argv[ 1 ]))) {
		error("gen_neg: [INPUT_DIR] does not exist.");
	}
	/* Set INPUT_PATH_BASE to [INPUT_DIR] and append '/' or '\' */
	strcpy(INPUT_PATH_BASE, argv[ 1 ]);
	sprintf(INPUT_PATH_BASE, "%s%c", INPUT_PATH_BASE, slash);

	if (!(dirout = opendir(argv[ 2 ]))) {
		error("gen_neg: [OUTPUT_DIR] does not exist.");
	}
	closedir(dirout); // not needed for output
	/* Set OUTPUT_PATH_BASE to [OUTPUT_DIR] and append '/' or '\' */
	strcpy(OUTPUT_PATH_BASE, argv[ 2 ]);
	sprintf(OUTPUT_PATH_BASE, "%s%c", OUTPUT_PATH_BASE, slash);

	/** Command is correct **/
	cout << "This program will take in large-sized images in [INPUT_DIR], then for each image:\n" <<
	" 1. Using a window of size: " << WINDOW_WIDTH << " x " << WINDOW_HEIGHT << "," <<
	" scan through the entire image.\n" <<
	" 2. Write the scanned subimages into [OUTPUT_DIR]/genCOUNT_TOTAL.jpg,\n" <<
	"    where COUNT_TOTAL is an integer counting from 1.\n\n" <<
	"Press [Enter] to continue, or ctrl+C/D/Z to exit ...";
	getchar();

	int COUNT_TOTAL = 0; // # of subimages generated by all files
	IplImage *img;
	/* For each image file in [INPUT_DIR] */
	while ((dp = readdir(dirin))) {
		/* The full input path is [INPUT_DIR] + slash + FILENAME */
		strcpy(INPUT_PATH, INPUT_PATH_BASE);
		sprintf(INPUT_PATH, "%s%s", INPUT_PATH, dp->d_name);
		cout << INPUT_PATH << "  ...  ";

		/* Read the image */
		if ((img = cvLoadImage(INPUT_PATH, -1))) {
			/* Success on image read */

			/* For different ROI's (Regions of Interest) */
			int COUNT_SINGLE = 0; // # of subimages generated by a single file
			for (int x = 0; x + WINDOW_WIDTH <= img->width; x += WINDOW_WIDTH) {
				for (int y = 0; y + WINDOW_HEIGHT <= img->height; y += WINDOW_HEIGHT, COUNT_SINGLE++) {
					//cout << "(" << x << "," << x + WINDOW_WIDTH - 1 << "," << y << "," << y + WINDOW_HEIGHT - 1 << ")\n";
					/* Set ROI on the window */
					cvSetImageROI(img, cvRect(x, y, WINDOW_WIDTH, WINDOW_HEIGHT));

					/* The full output path is [OUTPUT_DIR] + "/" + genCOUNT_TOTAL.jpg */
					strcpy(OUTPUT_PATH, OUTPUT_PATH_BASE);
					sprintf(OUTPUT_PATH, "%sgen%d.jpg", OUTPUT_PATH, ++COUNT_TOTAL);
					//cout << "OUTPUT_PATH: " << OUTPUT_PATH << endl;

					/* Write to file */
					if (!cvSaveImage(OUTPUT_PATH, img)) {
						cout << "cvSaveImage(): Failure\n";
						exit(EXIT_FAILURE);
					}
				}
			}
			cvReleaseImage(&img);
			cout << "done, generating " << COUNT_SINGLE << " subimages\n";
		}
		else {
			/* Failure on image read: skip this file */
			cout << "not an image\n";
		}
	}
	cout << "gen_neg(): Generated a total of " << COUNT_TOTAL << " images.\n";

	return 0;
}
